package app.web.cartoon.analyse.word;

import java.util.HashMap;
import java.util.Map;

import cn.lily.com.component.schema.table.multi.TMultiCell;
import cn.lily.com.component.schema.table.multi.auto.TMultiView;
import cn.lily.com.component.schema.table.multi.item.TMultiBody;
import cn.lily.com.component.schema.table.multi.item.TMultiTitle;
import cn.lily.com.container.data.ListTable;
import cn.lily.com.container.data.Record;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.exception.YaconException;
import cn.lily.com.lang.Basic;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.expand.grid.TMultiGrid;
import cn.lily.web.widget.standard.TButton;
import cn.lily.web.widget.standard.TButtonPilot;
/**
 * 动漫首字母分析页面
 * @author JiaJ
 * @date 2017年12月3日
 *
 */
public class CartoonWords extends BusinessPage {
	
	private Map<String,ListTable> dataMap = new HashMap<String,ListTable>();
	
	private Map<String,Record> dataRecord = new HashMap<String,Record>();

	@Override
	public void page_onLoad() throws YaconException {
		
		TMultiGrid cartoonWordGrid = new TMultiGrid("cartoonWordGrid");
		
		this.instData();
		
		this.instGrid(cartoonWordGrid);
		
		this.instData(cartoonWordGrid);
		
		this.getPage().addElement(cartoonWordGrid);
			
	}
	
	/**
	 * 初始化数据
	 */
	public void instData() {
		
		this.dataMap.clear();
		this.dataRecord.clear();
		
		String sql = "select lci.CAN_FIRST,lci.CAN_NAME,to_number(ldi.CAN_TOTAL) CAN_TOTAL,lci.CAN_ID "
				   + "from ${schema}.LEO_CAN_INFO lci "
				   + "inner join ${schema}.LEO_CAN_DETAIL_INFO ldi on lci.CAN_ID=ldi.CAN_ID ";
		
		sql = "select CAN_FIRST,CAN_NAME,CAN_ID,sum(CAN_TOTAL) as CAN_TOTAL "
			 + "from ("+sql+") bb "
			 + "group by bb.CAN_FIRST,bb.CAN_NAME,bb.CAN_ID ";
		
		sql = "select * from ("+sql+") order by CAN_TOTAL DESC";
		
		DataTable dt = DBFactory.getActionDB().executeQueryTable(sql);
		
		while(dt.next()){
			
			String firstWord = dt.getString("CAN_FIRST");
			
			int total = Basic.getInt(dt.getString("CAN_TOTAL"));
			total = total == 0 ? 1 : total;
			
			String canName = dt.getString("CAN_NAME").trim();
			String canId = dt.getString("CAN_ID").trim();
			
			//字母表
			ListTable activeTable = this.dataMap.get(firstWord);
			if(activeTable == null){
				activeTable = new ListTable(firstWord);
				this.dataMap.put(firstWord, activeTable);
			}
			
			//记录
			Record record = this.dataRecord.get(canId);
			if(record == null){
				record = activeTable.createRecord();				
				this.dataRecord.put(canId, record);
			}
			
			//存储信息
			record.setFieldValue("FIRST_WORD", firstWord);
			record.setFieldValue("CAN_NAM", canName);
			
			//总数
			String count = record.getString("CAN_COUNT");
			if(count == null || "".equals(count)){
				record.setFieldValue("CAN_COUNT", "1");
			}else{			
				record.setFieldValue("CAN_COUNT", Basic.getInt(count)+1);			
			}
			
			//总集数
			String oldtotal = record.getString("CAN_TOTAL");
			if(oldtotal == null || "".equals(oldtotal)){
				record.setFieldValue("CAN_TOTAL", total);
			}else{
				record.setFieldValue("CAN_TOTAL", Basic.getInt(oldtotal)+total);
			}
			
		}
		
	}
	
	/**
	 * 初始化标题
	 * @param cartoonFanGrid
	 */
	public void instGrid(TMultiGrid cartoonWordGrid){
		
		String sql = "select * from LEO_CAN_INFO";
		String sqlx = "select * from LEO_CAN_DETAIL_INFO";
		
		int allCount = DBFactory.getActionDB().RecordCount(sql);
		int detailCount = DBFactory.getActionDB().RecordCount(sqlx);
		
		TMultiTitle title = cartoonWordGrid.getTitle();
		
		title.createCell(0, 0, 1, 5).setFieldName("tagInfo").setFieldValue("动漫首字母分析表【"+allCount+"-"+detailCount+"】");
			
		/* 设置标题 */
		TButtonPilot cartoonWordPilot = ButtonPilot("cartoonWordPilot");
		cartoonWordPilot.setPilotIco("/widget/buttonPilot.gif");
		cartoonWordPilot.setPilotText("动漫首字母信息");
		cartoonWordPilot.addButtons(new TButton("refresh"));
		
		cartoonWordGrid.setButtonPilot(cartoonWordPilot);
		
		//设置双击事件
		cartoonWordGrid.setDataDbClick("cartoonWordGrid_dbclick");
		
	}
	
	/**
	 * 数据加载
	 * @param testGrid
	 */
	public void instData(TMultiGrid testGrid){

		TMultiBody body = testGrid.getBody();
		
		int rowIdx = -1;
		int colIdx = 0;
		
		String allWord = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		
		int wordLength = allWord.length();
		for(int i=0;i<wordLength;i++){
			
			//初始化行号、列号
			rowIdx++ ;
			colIdx = 0;
			
			String active = allWord.charAt(i)+"";			
					
			//数据循环
			ListTable tables = this.dataMap.get(active);
			
			if(tables == null){
				body.createDataCell(rowIdx, 0,1,5).setFieldValue(active+"【0部】");
			}else{
				body.createDataCell(rowIdx, 0,1,5).setFieldValue(active+"【"+tables.getRecordCount()+"部】");
			}
				
			this.setTitleColor(body,rowIdx);
							
			if(tables == null){	
			
				rowIdx++;
				this.setBlankColor(body,rowIdx);
				continue;
			}
			
			while(tables.next()){
				
				if(colIdx % 5 == 0){
	
					rowIdx++;
				}
				
				String name = tables.getString("CAN_NAM");
				int total = tables.getInt("CAN_TOTAL");
				body.createDataCell(rowIdx,colIdx % 5).setFieldValue(name+"-"+total+"话");	
				this.setColor(body.getDataCell(rowIdx, colIdx % 5), total);
				colIdx++;
			}
			
			int result = colIdx % 5;
			if(result != 0){
				for(;result<5;result++){
					this.setColor(body.createDataCell(rowIdx, result), -1);
				}
			}
							
			rowIdx++;	
			this.setBlankColor(body,rowIdx);
			
		}
		
	}
	
	public void setTitleColor(TMultiBody body,int rowIdx){
		

		body.getDataCell(rowIdx, 0).setBackColor("#D6D6D6").setFontColor("#000000");
		

	}
	
	public void setBlankColor(TMultiBody body,int rowIdx){
		
		body.createDataCell(rowIdx, 0 , 1 , 5).setFieldValue("&nbsp;").setBackColor("#FFFFFF");
		
	}
	
	public void setColor(TMultiCell cell,int count){
		
		if(count < 0){
			cell.setBackColor("#FFFFFF");
			cell.setFontColor("#000000");
		}
		
		//超短篇 -- 绿色
		else if(count>=0 && count < 4){
			cell.setBackColor("#CAFF70");
			cell.setFontColor("#000000");
		}
		
		//季番 -- 蓝色
		else if(count >=4 && count<16){
			cell.setBackColor("#E0FFFF");	
			cell.setFontColor("#000000");
		}
		
		//半年番 -- 黄色
		else if(count >=16 && count < 31){
			cell.setBackColor("#FFFF00");
			cell.setFontColor("#000000");
		}
		
		//年番 --橙色
		else if(count >=31 && count < 75){
			cell.setBackColor("#FF8C00");
			cell.setFontColor("#000000");
		}
		
		//两年番 -- 粉色
		else if(count >=75 && count < 129){
			cell.setBackColor("#FF83FA");
			cell.setFontColor("#000000");		
		}
		
		//长篇 -- 红色
		else{
			cell.setBackColor("#FF3030");
			cell.setFontColor("#000000");
		}
	}

	/**
	 * null转换为空字符串
	 * @param str
	 * @return
	 */
	public String getString(String str){
		if(str == null){
			return "";
		}
		return str;
	}
	

	public void refresh() throws YaconException {
		
		TMultiGrid cartoonWordGrid = new TMultiGrid("cartoonWordGrid");
		
		this.instData();
		
		this.instGrid(cartoonWordGrid);
		
		this.instData(cartoonWordGrid);
			
		this.setSelfMessage(cartoonWordGrid.getDataElement());
		
	}

}
